package com.guowen.myaccount;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.guowen.myaccount.adapter.AccManagementAdapter;
import com.guowen.myaccount.db.AccManageBean;
import com.guowen.myaccount.db.DBManager;
import com.guowen.myaccount.utils.SelectTimeDialog;
import com.guowen.myaccount.utils.TranAccountDialog;

import java.util.List;

public class TranAccountActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView inAccTv, outAccTv, timeTv, ensureTv;
    private EditText moneyEt, beizhuEt;
    private ImageView backIv;
    private AccManageBean inBean, outBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tran_account);

        initView();
    }

    private void initView() {
        inAccTv = findViewById(R.id.activity_tran_acc_in_account);
        outAccTv = findViewById(R.id.activity_tran_acc_out_account);
        timeTv = findViewById(R.id.activity_tran_acc_time);
        ensureTv = findViewById(R.id.activity_tran_acc_ensure);
        moneyEt = findViewById(R.id.activity_tran_acc_money);
        beizhuEt = findViewById(R.id.activity_tran_acc_beizhu);
        backIv = findViewById(R.id.activity_tran_acc_back);

        backIv.setOnClickListener(this);
        inAccTv.setOnClickListener(this);
        outAccTv.setOnClickListener(this);
        ensureTv.setOnClickListener(this);
        timeTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        TranAccountDialog dialog = null;
        switch (v.getId()) {
            case R.id.activity_tran_acc_back:
                finish();
                break;
            case R.id.activity_tran_acc_in_account:
                dialog = new TranAccountDialog(this);
                dialog.show();
                dialog.setOnEnsureListener(new TranAccountDialog.OnEnsureListener() {
                    @Override
                    public void onEnsure(AccManageBean bean) {
                        inAccTv.setText(bean.getTypename());
                        inBean = bean;
                    }
                });
                break;
            case R.id.activity_tran_acc_out_account:
                dialog = new TranAccountDialog(this);
                dialog.show();
                dialog.setOnEnsureListener(new TranAccountDialog.OnEnsureListener() {
                    @Override
                    public void onEnsure(AccManageBean bean) {
                        outAccTv.setText(bean.getTypename());
                        outBean = bean;
                    }
                });
                break;
            case R.id.activity_tran_acc_time:
                showTimeDialog();
                break;
            case R.id.activity_tran_acc_ensure:
                String inAccName = inAccTv.getText().toString();
                String outAccName = outAccTv.getText().toString();
                String moneyStr = moneyEt.getText().toString();
                float money = 0;
                if (!moneyStr.isEmpty()){
                    money = Float.parseFloat(moneyStr);
                }

                if (!inAccName.isEmpty() && !outAccName.isEmpty()){

                    if (inAccName.equals(outAccName)){
                        Toast.makeText(this,"请选择不同的账户之间进行转账！",Toast.LENGTH_SHORT).show();
                    }else{
                        inBean.setMoney(inBean.getMoney()+money);
                        outBean.setMoney(outBean.getMoney()-money);
                        DBManager.alterItemFromAccmanagetbById(inBean.getId(),inBean);
                        DBManager.alterItemFromAccmanagetbById(outBean.getId(),outBean);
                        List<AccManageBean> mDatas = DBManager.getAccManList();
                        AccManagementActivity.adapter = new AccManagementAdapter(AccManagementActivity.context,mDatas);
                        AccManagementActivity.accManageLv.setAdapter(AccManagementActivity.adapter);
                        AccManagementActivity.adapter.notifyDataSetChanged();
                        finish();
                    }
                }else{
                    Toast.makeText(this,"请先完成账户选择！",Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }

    private void showTimeDialog() {
        SelectTimeDialog dialog = new SelectTimeDialog(this);
        dialog.show();
        //设定确定按钮被点击了的监听器
        dialog.setOnEnsureListener(new SelectTimeDialog.OnEnsureListener() {
            @Override
            public void onEnsure(String time, int year, int month, int day) {
                timeTv.setText(year+"-"+month+"-"+day);
            }
        });
    }
}